package news.paper.tintuc.tinnong.docbao.doctin.adapter;

import java.util.ArrayList;

import news.paper.tintuc.tinnong.docbao.doctin.R;
import news.paper.tintuc.tinnong.docbao.doctin.controller.Controller;
import news.paper.tintuc.tinnong.docbao.doctin.controller.InterfaceAdapter;
import news.paper.tintuc.tinnong.docbao.doctin.controller.RequestListener;
import news.paper.tintuc.tinnong.docbao.doctin.model.News;
import news.paper.tintuc.tinnong.docbao.doctin.util.Constrant;

import org.json.JSONArray;
import org.json.JSONObject;

import tienich360.com.lib.JsonConvert;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

@SuppressLint({ "ViewHolder", "InflateParams" })
public class AdapterNews extends BaseAdapter {

	private Activity activity;
	private ArrayList<News> arrayList;
	private static LayoutInflater inflater = null;
	private int page;
	private boolean isloading;
	private InterfaceAdapter interfaceAdapter;
	private News news;

	DisplayImageOptions options;

	public AdapterNews(Activity a, InterfaceAdapter interfaceAdapter_) {
		activity = a;
		page = 0;
		isloading = false;
		arrayList = new ArrayList<News>();
		interfaceAdapter = interfaceAdapter_;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		options = new DisplayImageOptions.Builder()
				 .showImageOnLoading(R.drawable.null2)
				 .showImageForEmptyUri(R.drawable.null2)
				 .showImageOnFail(R.drawable.null2)
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	public int getCount() {
		return arrayList.size();
	}

	public Object getItem(int position) {
		return arrayList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	private static class ViewHolderItem {
		private TextView title;
		private TextView artist;
		private TextView duration;
		private ImageView thumb_image;

	}

	@SuppressWarnings("deprecation")
	public View getView(final int position, View vi, ViewGroup parent) {
		ViewHolderItem viewHolder;

		if (vi == null) {
			vi = inflater.inflate(R.layout.list_row, null);

			viewHolder = new ViewHolderItem();
			viewHolder.title = (TextView) vi.findViewById(R.id.title22);
			viewHolder.artist = (TextView) vi.findViewById(R.id.artist);
			viewHolder.duration = (TextView) vi.findViewById(R.id.duration);
			viewHolder.thumb_image = (ImageView) vi
					.findViewById(R.id.imgNetwork);
			
			vi.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolderItem) vi.getTag();
		}

		News song = arrayList.get(position);
		try {
			viewHolder.title.setTextSize(Constrant.TEXT_SIZE);
			viewHolder.artist.setTextSize(Constrant.TEXT_SIZE);
			viewHolder.duration.setTextSize(Constrant.TEXT_SIZE);
			viewHolder.title.setText(song.getTitle());
			viewHolder.artist.setText("Nguồn: " + song.getSourceName() + " - "
					+ song.getDate());
			viewHolder.duration.setText(song.getDescription());
			String image_link = "";
			int hight = 0;
			if (song.getLandscapeAvatarHeight() == 0) {
				image_link = song.getPortraitAvatar();
				hight = Constrant.WIDTH * song.getPortraitAvatarHeight()
						/ song.getPortraitAvatarWidth();
			} else {
				image_link = song.getLandscapeAvatar();
				hight = Constrant.WIDTH * song.getLandscapeAvatarHeight()
						/ song.getLandscapeAvatarWidth();
			}

			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
					LayoutParams.FILL_PARENT, hight);
			viewHolder.thumb_image.setLayoutParams(layoutParams);
			ImageLoader.getInstance().displayImage(image_link,
					viewHolder.thumb_image, options);
		} catch (Exception e) {
		}

		if (position == getCount() - 1 && !isloading) {
			load_data(Constrant.SUBID);
		}
		return vi;
	}

	public void re_int() {
		arrayList.clear();
		page = 0;
		notifyDataSetChanged();
		load_data(Constrant.SUBID);
	}

	public void re_int(int index) {
		news = Constrant.arrayListNews.get(index);
		arrayList.clear();
		page = 0;
		notifyDataSetChanged();
		load_data(Constrant.SUBID);
	}

	public void load_data(String subid) {
		Controller controller = new Controller();
		if (news == null) {
			news = new News();
			news.setListType("specialzone");
			news.setListId(2);
		}
		// if (news == null && Constrant.arrayListNews.size() > 0) {
		// news = Constrant.arrayListNews.get(0);
		// } else {
		// news = new News();
		// news.setListType("specialzone");
		// news.setListId(2);
		// }

		controller.getListNews(news.getListType(), news.getListId() + "", page
				+ "", (page + Controller.perpage) + "", new RequestListener() {

			@Override
			public void success(Object response) {
				try {
					JSONObject jsonObject = (JSONObject) response;
					arrayList = JsonConvert.DeserializeArray(News.class,
							(JSONArray) jsonObject.getJSONArray("articlelist"),
							arrayList);
					notifyDataSetChanged();
				} catch (Exception e) {
					e.printStackTrace();
				}
				isloading = false;
				interfaceAdapter.finish_load();
			}

			@Override
			public void fail(String message) {
				isloading = false;
				interfaceAdapter.finish_load();
			}

			@Override
			public void begin() {
				isloading = true;
				interfaceAdapter.start_load();
			}
		});
		page += Controller.perpage;
	}
}